<?php
/**
 * ProductsController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-14 01:24:38
 * @modified    2023-02-14 01:24:38
 *
 */

namespace App\Http\Controllers;

use App\Repositories\ProductRepo;
use Illuminate\Http\Request;
use App\Services\ProductService;

class ProductsController extends BaseController
{


    /**
     * 构造函数
     * @author aaron
     * @since 2021/6/9
     * ArticleController constructor.
     */

    public function __construct()
    {
        parent::__construct();
        $this->service = new ProductService();
    }

    public function params(Request $request)
    {
        $params = ProductRepo::params();
        return message("获取成功", true, $params);
    }

    /**
     * @OA\Get(
     *     path="/products",
     *     tags={"Product"},
     *     summary="获取商品列表",
     *     description="获取商品列表",
     *     operationId="product_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="name", in="query", description="产品名称", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="model", in="query", description="产品型号", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="category_id", in="query", description="产品分类ID", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="brand_id", in="query", description="品牌ID", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="filter_project", in="query", description="过滤已绑定项目", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10,type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index(Request $request)
    {
        $products = ProductRepo::getList();
        return list_message("获取成功", true, $products);
    }

    /**
     * @OA\Get(
     *     path="/products/{id}",
     *     tags={"Product"},
     *     summary="获取商品详情",
     *     description="获取商品详情",
     *     operationId="product_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="商品ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function show(Request $request, $id)
    {
        try {
            $product = ProductRepo::fetch($id);
            return message("获取成功", true, $product);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/products",
     *     tags={"Product"},
     *     summary="添加商品",
     *     description="添加商品",
     *     operationId="product_store",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"customer_id", "type_id", "if_qrcode", "name", "model", "attachment", "warranty_type", "warranty_start", "warranty_end", "date_buy", "date_check"},
     *              @OA\Property(property="event_id", type="integer", example="事件ID（事件转工单时，如果新建，需要更新事件的产品ID）", description="事件ID（事件转工单时，如果新建，需要更新事件的产品ID）"),
     *              @OA\Property(property="customer_id", type="integer", example=0, description="所属客户ID"),
     *              @OA\Property(property="type_id", type="integer", example=0, description="商品类型ID"),
     *              @OA\Property(property="if_qrcode", type="integer", example=0, description="是否生成二维码"),
     *              @OA\Property(property="name", type="string", example="", description="商品名称"),
     *              @OA\Property(property="model", type="string", example="", description="商品型号"),
     *              @OA\Property(property="attachment", type="string", example="", description="出厂报告地址"),
     *              @OA\Property(property="install_address", type="string", example="", description="设备安装地址"),
     *              @OA\Property(property="warranty_type", type="string", example="", description="质保计算方式"),
     *              @OA\Property(property="warranty_start", type="string", example="", description="质保开始时间"),
     *              @OA\Property(property="warranty_end", type="string", example="", description="质保结束时间"),
     *              @OA\Property(property="date_buy", type="string", example="", description="购买日期"),
     *              @OA\Property(property="date_check", type="string", example="", description="验收日期")
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store(Request $request)
    {
        try {
            $bom = ProductRepo::store(0);
            return message("创建成功", true, $bom);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Put(
     *     path="/products/{id}",
     *     tags={"Product"},
     *     summary="更新商品",
     *     description="更新商品",
     *     operationId="product_update",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="商品ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"customer_id", "type_id", "if_qrcode", "name", "model", "attachment", "warranty_type", "warranty_start", "warranty_end", "date_buy", "date_check"},
     *              @OA\Property(property="customer_id", type="integer", example=0, description="所属客户ID"),
     *              @OA\Property(property="type_id", type="integer", example=0, description="商品类型ID"),
     *              @OA\Property(property="if_qrcode", type="integer", example=0, description="是否生成二维码"),
     *              @OA\Property(property="name", type="string", example="", description="商品名称"),
     *              @OA\Property(property="model", type="string", example="", description="商品型号"),
     *              @OA\Property(property="attachment", type="string", example="", description="出厂报告地址"),
     *              @OA\Property(property="install_address", type="string", example="", description="设备安装地址"),
     *              @OA\Property(property="warranty_type", type="string", example="", description="质保计算方式"),
     *              @OA\Property(property="warranty_start", type="string", example="", description="质保开始时间"),
     *              @OA\Property(property="warranty_end", type="string", example="", description="质保结束时间"),
     *              @OA\Property(property="date_buy", type="string", example="", description="购买日期"),
     *              @OA\Property(property="date_check", type="string", example="", description="验收日期")
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function update(Request $request, $id)
    {
        try {
            $product = ProductRepo::store($id);
            return message("更新成功", true, $product);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Delete(
     *     path="/products/{id}",
     *     tags={"Product"},
     *     summary="删除商品",
     *     description="删除商品",
     *     operationId="product_destroy",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="商品ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function destroy(Request $request, $id)
    {
        try {
            ProductRepo::destroy($id);
            return message("删除成功", true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/products_export",
     *     tags={"Product"},
     *     summary="导出商品数据",
     *     description="导出商品数据",
     *     operationId="products_export",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function export(Request $request)
    {
        try {
            $url = ProductRepo::export();
            return message('导出成功', true, ['url' => $url]);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }
}
